package com.oriental.manage.controller.settlement.clear.detail;

import com.oriental.manage.core.excelUtils.ExcelUtils;
import com.oriental.manage.core.fileUtils.ExcelContentExt;
import com.oriental.manage.core.fileUtils.ExtExcelWriterCallBack;
import com.oriental.manage.core.fileUtils.FileDownAjax;
import com.oriental.manage.core.fileUtils.HeaderExt;
import com.oriental.manage.core.paging.Pagination;
import com.oriental.manage.core.response.ResponseDTO;
import com.oriental.manage.core.utils.Bean2MapUtil;
import com.oriental.manage.core.utils.DateUtils;
import com.oriental.manage.core.utils.StringCommonUtils;
import com.oriental.manage.pojo.merchant.settleManage.MchntPayableamtManage;
import com.oriental.manage.service.settlement.MchntPayableAmtService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.webbuilder.office.excel.api.poi.POIExcelApi;
import org.webbuilder.office.excel.config.ExcelWriterConfig;
import org.webbuilder.office.excel.config.Header;

import java.io.File;
import java.io.FileOutputStream;
import java.math.BigDecimal;
import java.util.*;

/**
 * Author: yutao
 * Date: 2017/5/23
 * Time: 14:48
 * Desc：
 */
@Controller
@Slf4j
@RequestMapping("mchnt/mchntPayableAmtManage")
public class MchntPayableAmtManController {

    @Autowired
    private MchntPayableAmtService mchntPayableAmtService;

    @Autowired
    private FileDownAjax fileDownAjax;

    @RequestMapping("init")
    public String init(){
        return "settlement/clear/detail/mchntPayableamtManage";
    }

    @RequestMapping("search")
    @ResponseBody
    public ResponseDTO<Pagination<MchntPayableamtManage>> query(@RequestBody Pagination<MchntPayableamtManage> pagination){
        ResponseDTO<Pagination<MchntPayableamtManage>> responseDTO = new ResponseDTO<>();
        try{
            log.info("商户应付款结算管理查询请求参数:{}",pagination);
            mchntPayableAmtService.query(responseDTO,pagination);

        }catch (Exception e){
            log.error("查询失败", e);
            responseDTO.setSuccess(false);
            responseDTO.setMsg("系统繁忙");
        }
        return responseDTO;
    }


    @RequestMapping("download")
    public ResponseEntity<byte[]> download(@RequestBody MchntPayableamtManage mchntPayableamtManage){
        ResponseEntity<byte[]> responseEntity = null;
        File tempFile = null;
        try{
            tempFile = fileDownAjax.touch("商户应付款结算管理" + DateUtils.now() + ".xlsx");
            String benginDate = DateUtils.format(mchntPayableamtManage.getQueryStartDate(),DateUtils.DATESHORTFORMAT);
            String endDate= DateUtils.format(mchntPayableamtManage.getQueryEndDate(),DateUtils.DATESHORTFORMAT);
            ResponseDTO<Pagination<MchntPayableamtManage>> responseDTO = new ResponseDTO<>();
            Pagination<MchntPayableamtManage> pagination = new Pagination<>();
            pagination.setQueryBean(mchntPayableamtManage);
            pagination.getQueryBean().setPageNum(1);
            pagination.getQueryBean().setPageSize(10000);
            mchntPayableAmtService.query(responseDTO,pagination);
            List<MchntPayableamtManage> list = pagination.getList();

            //表头
            LinkedList<Header> tHeaders = new LinkedList<>();
            //内容
            List<Object> datas = new ArrayList<>();
            //添加表头
            tHeaders.add(new HeaderExt("序号", "index"));
            tHeaders.add(new HeaderExt("外部清算日期", "transDate"));
            tHeaders.add(new HeaderExt("商户名称", "orgCode", ExcelContentExt.MCHNT));
            tHeaders.add(new HeaderExt("类型", "type"));
            tHeaders.add(new HeaderExt("销账净额（元）", "sttleAmt",ExcelContentExt.CURRENCY));
            tHeaders.add(new HeaderExt("结算状态", "clrStatus"));
            tHeaders.add(new HeaderExt("确认日期", "confirmTime"));
            tHeaders.add(new HeaderExt("实付日期", "bankTransPayDate"));
            int i=1;
            if(responseDTO.isSuccess()){
                BigDecimal payableAmt=null;
                BigDecimal receivableAmt=null;
                for(MchntPayableamtManage mchntPayableamtManage1:list){
                    payableAmt= new BigDecimal(mchntPayableamtManage1.getPayableAmt());
                    receivableAmt = new BigDecimal(mchntPayableamtManage1.getReceivableAmt());
                    Map map = Bean2MapUtil.convertBean(mchntPayableamtManage1);
                    map.put("sttleAmt",payableAmt.subtract(receivableAmt));
                    if(mchntPayableamtManage1.getClrStatus().equals("1")){
                        map.put("clrStatus","已结算");
                    }
                    if(mchntPayableamtManage1.getClrStatus().equals("0")){
                        map.put("clrStatus","未结算");
                    }
                    if(mchntPayableamtManage1.getType().equals("0")){
                        map.put("type","邮局");
                    }
                    if(mchntPayableamtManage1.getType().equals("1")){
                        map.put("type","非邮局");
                    }

                    if(mchntPayableamtManage1.getBankTransRecDate() !=null && mchntPayableamtManage1.getBankTransPayDate() ==null){
                        map.put("bankTransPayDate",mchntPayableamtManage1.getBankTransRecDate());
                    }

                    if(mchntPayableamtManage1.getBankTransPayDate() !=null && mchntPayableamtManage1.getBankTransRecDate() ==null){
                        map.put("bankTransPayDate",mchntPayableamtManage1.getBankTransPayDate());
                    }

                    if(mchntPayableamtManage1.getBankTransPayDate() !=null && mchntPayableamtManage1.getBankTransRecDate() !=null){
                        map.put("bankTransPayDate",mchntPayableamtManage1.getBankTransPayDate());
                    }

                    map.put("index",i);
                    i++;
                    datas.add(map);
                }
                Map m = new HashMap();

                m.put("type","总计");
                String sumObject = (String)responseDTO.getSumObject();
                if(sumObject.equals("")){
                    m.put("sttleAmt",new BigDecimal(0));
                }else{
                    m.put("sttleAmt",new BigDecimal(sumObject));
                }
                datas.add(m);

            }
            ExcelWriterConfig config = new ExcelWriterConfig();
            config.setHeaders(tHeaders);
            config.setDatas(datas);
            ExtExcelWriterCallBack ca = new ExtExcelWriterCallBack(config);
            FileOutputStream outputStream = new FileOutputStream(tempFile);
            POIExcelApi.getInstance().write(outputStream, ca);
            File file = ExcelUtils.addTitle(tempFile, "商户应付款结算管理报表", fileDownAjax, tHeaders.size(), benginDate, endDate);
            responseEntity = fileDownAjax.getResponseEntity(file);

        }catch (Exception e){
            log.error("下载失败", e);
            responseEntity = fileDownAjax.getResponseEntityFail();
        }finally {
            fileDownAjax.forceDelete(tempFile);
        }
        return responseEntity;
    }



}
